﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.StaffPages
{
    /// <summary>
    /// Rehab 的摘要说明。
    /// </summary>
    public partial class RehabPage : Page
    {
        protected static string GetSex(string sex, string id)
        {
            if (id != "")
                return ((sex == "True") ? "女" : "男");
            return ("");
        }

        protected static string GetAge(string birthday)
        {
            if ((birthday != "") && (DateTime.Parse(birthday) != DateTime.Parse("1900-1-1")))
                return ((DateTime.Now.Year - (DateTime.Parse(birthday)).Year + 1).ToString());
            return ("");
        }

        private void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (!Page.IsPostBack)
            {
                BindGrid();
            }
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        private void BindGrid()
        {
            var db = new Database();
            DataTable dt;
            using (var dr = db.RunDataReader("dbo.sproc_GetDimissionStaff"))
            {
                dt = Tools.ConvertDataReaderToDataTable(dr);
            }

            //在DataTable的末尾加上空行，使得DataGrid固定行数
            int blankrows = StaffList.PageSize - (dt.Rows.Count % StaffList.PageSize);
            for (int i = 0; i < blankrows; i++)
            {
                dt.Rows.Add(dt.NewRow());
            }

            StaffList.DataSource = dt.DefaultView;
            StaffList.DataBind();

            //对于空纪录不显示checkbox
            for (int i = 0; i < StaffList.Items.Count; i++)
            {
                if (StaffList.Items[i].Cells[1].Text == "&nbsp;")
                {
                    StaffList.Items[i].FindControl("cb_StaffID").Visible = false;
                }
            }
            lbl_totalrecord.Text = StaffList.PageCount.ToString();
            lbl_curpage.Text = txb_PageNo.Text = (StaffList.CurrentPageIndex + 1).ToString();
            txb_ItemPerPage.Text = StaffList.PageSize.ToString();
            lbl_totalpage.Text = StaffList.PageCount.ToString();
        }

        private void PagerButtonClick(object sender, ImageClickEventArgs e)
        {
            //获得LinkButton的参数值
            String arg = ((ImageButton)sender).CommandArgument;

            switch (arg)
            {
                case ("next"):
                    if (StaffList.CurrentPageIndex < (StaffList.PageCount - 1))
                    {
                        StaffList.CurrentPageIndex++;
                    }
                    break;
                case ("pre"):
                    if (StaffList.CurrentPageIndex > 0)
                    {
                        StaffList.CurrentPageIndex--;
                    }
                    break;
                case ("first"):
                    StaffList.CurrentPageIndex = 0;
                    break;
                case ("last"):
                    StaffList.CurrentPageIndex = (StaffList.PageCount - 1);
                    break;
                default:
                    //本页值
                    StaffList.CurrentPageIndex = Convert.ToInt32(arg);
                    break;
            }
            BindGrid();
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            //页面直接跳转的代码
            if (txb_PageNo.Text.Trim() != "")
            {
                int PageI = Int32.Parse(txb_PageNo.Text.Trim()) - 1;
                if (PageI >= 0 && PageI < (StaffList.PageCount))
                {
                    StaffList.CurrentPageIndex = PageI;
                }
            }
            BindGrid();
        }

        private void txb_ItemPerPage_TextChanged(object sender, EventArgs e)
        {
            if (txb_ItemPerPage.Text.Trim() != "")
            {
                int itemPage = Int32.Parse(txb_ItemPerPage.Text.Trim());
                if (itemPage > 0)
                {
                    StaffList.PageSize = Int32.Parse(txb_ItemPerPage.Text.Trim());
                }
            }
            BindGrid();
        }

        private string GetSelectedItemID(string controlID)
        {
            string selectedID = "";
            //遍历DataGrid获得checked的ID
            foreach (DataGridItem item in StaffList.Items)
            {
                if (((CheckBox)item.FindControl(controlID)).Checked)
                {
                    selectedID += item.Cells[1].Text.Trim() + ",";
                }
            }
            selectedID = selectedID.Substring(0, selectedID.Length - 1);
            return selectedID;
        }

        private void cmdChangeDepartment_ServerClick(object sender, EventArgs e)
        {
            Response.Redirect("../Department/ChangeDepartment.aspx?BackFilePath=" + Request.CurrentExecutionFilePath + "&StaffIDS=" + GetSelectedItemID("cb_StaffID"));
        }

        private void cmdRestoreDocument_ServerClick(object sender, EventArgs e)
        {
            var st = new Components.Staff();
            if (st.ReturnPosition(GetSelectedItemID("cb_StaffID")) == false)
            {
                Server.Transfer("../Error.aspx");
            }
            else
            {
                Response.Redirect("Rehab.aspx");
            }
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.btn_first.Click += new System.Web.UI.ImageClickEventHandler(this.PagerButtonClick);
            this.btn_pre.Click += new System.Web.UI.ImageClickEventHandler(this.PagerButtonClick);
            this.btn_next.Click += new System.Web.UI.ImageClickEventHandler(this.PagerButtonClick);
            this.btn_last.Click += new System.Web.UI.ImageClickEventHandler(this.PagerButtonClick);
            this.btn_Go.ServerClick += new System.EventHandler(this.btnGo_Click);
            this.txb_ItemPerPage.TextChanged += new System.EventHandler(this.txb_ItemPerPage_TextChanged);
            this.cmdChangeDepartment.ServerClick += new System.EventHandler(this.cmdChangeDepartment_ServerClick);
            this.cmdRestoreDocument.ServerClick += new System.EventHandler(this.cmdRestoreDocument_ServerClick);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion
    }
}